var express = require('express');
var router = express.Router();
var dynamic = require('../db/dynamic.js');
var User = require('../db/user.js');
var tools = require('../units/tools.js');


//动态里的图片删除
router.post("/deleteDyPic", function (req, res, next) {
    // console.log(req.body.picUrl);
    dynamic.updateOne({ d_id: req.body.did }, { $pull: { pictureUrl: { url: req.body.picUrl } } }, function (err) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.send("删除成功！");
        }
    });
});

//动态里的图片上传
router.post("/imgDynamic", tools.multer().single("PicturesOfDy"), function (req, res, next) {
    // console.log(req.body.did);
    let imgD = req.file.path.split("\\");
    let imgUrl = "http://localhost:3000/" + imgD[1] + "/" + imgD[2] + "/" + imgD[3];
    dynamic.updateOne({ d_id: req.body.did }, { $addToSet: { pictureUrl: { url: imgUrl } } }, function (err) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.send("图片已添加");
        }
    });
});

//发布动态
router.post("/openDynamic", function (req, res, next) {
    let did = req.body.did;
    let uptime = req.body.showTime;
    let Mscontent = req.body.content;
    dynamic.updateOne({ d_id: did }, { showTime: uptime, content: Mscontent, d_type: true }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            res.send("发布成功");
        }
    });
});

//返回动态信息
router.post("/getDynamic", function (req, res, next) {
    let did = req.body.did;
    dynamic.findOne({ d_id: did }, function (err, data) {
        if (err) { console.log(err); return; } else {
            // console.log(data);
            if (data.d_type == true) {
                res.send({
                    title: data.showTime,
                    content: data.content,
                    picList: data.pictureUrl,
                });
            } else {
                res.send({
                    title: data.createTime,
                    content: data.content,
                    picList: data.pictureUrl,
                });
            }
        }
    });
});

//删除动态
router.post("/deleteDynamic", function (req, res, next) {
    let did = req.body.did;
    dynamic.deleteOne({ d_id: did }, function (err) {
        if (err) { console.log(err); return; }
        else {
            res.send("删除成功");
        }
    })
});

//返回动态列表信息
router.post("/getDynamicList", function (req, res, next) {
    dynamic.find({ uid: req.cookies.userID }, function (err, data) {
        if (err) { console.log(err); return; }
        else {
            // console.log(data);
            let MsList = [];
            for (let i = data.length - 1; i >= 0; i--) {
                if (data[i].showTime != null) {
                    MsList.push({
                        did: data[i].d_id,
                        title: data[i].showTime,
                        type: data[i].d_type,
                    })
                } else {
                    MsList.push({
                        did: data[i].d_id,
                        title: data[i].createTime,
                        type: data[i].d_type,
                    })
                }
            };
            res.send(MsList);
        }
    });
});

//创建动态
router.post("/putDynamicList", function (req, res, next) {
    let createtime = req.body.title;
    User.findOne({ uid: req.cookies.userID }, function (err, item) {
        if (err) { console.log(err); return; }
        let Ms = dynamic({
            uid: req.cookies.userID,
            uname: item.uname,
            d_id: "Ms/" + req.cookies.userID + "/" + createtime,
            createTime: createtime,
            d_type: false,
        });
        Ms.save(function (err) {
            if (err) {
                console.log(err);
            } else {
                // console.log(datas);
                res.send("新建动态成功");
            }
        });
    })
});

module.exports = router